package com.xyazm.inventory.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyazm.common.dao.BaseDAO;
import com.xyazm.inventory.domain.entity.ProductStockDO;
import com.xyazm.inventory.mapper.ProductStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 库存
 * @author xuyou
 * 2022/6/4 0004 22:45
*/
@Repository
public class ProductStockDAO extends BaseDAO<ProductStockMapper, ProductStockDO> {

    @Autowired
    private ProductStockMapper productStockMapper;


    /**
     * 根据skuCode查询商品库存记录
     * @param skuCode
     * @return
     */
    public ProductStockDO getBySkuCode(String skuCode) {
        QueryWrapper<ProductStockDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_code", skuCode);
        return getOne(queryWrapper);
    }

    /**
     * 锁定商品库存
     * @param skuCode
     * @param saleQuantity
     * @return
     */
    public int lockProductStock(String skuCode, Integer saleQuantity) {
        return productStockMapper.lockProductStock(skuCode, saleQuantity);
    }

    /**
     * 释放商品库存
     * @author xuyou
     * @date: 2022/7/1 14:05
     */
    public int releaseProductStock(String skuCode, Integer saleQuantity) {
        return productStockMapper.releaseProductStock(skuCode, saleQuantity);
    }


    /**
     * 扣减销售库存
     * @param skuCode
     * @param saleQuantity
     * @return
     */
    public int deductSaleStock(String skuCode, Integer saleQuantity) {
        return productStockMapper.deductSaleStock(skuCode,saleQuantity);
    }

    /**
     * 增加销售库存
     * @author xuyou
     * 2022/8/6 0006 22:32
     * @param skuCode
     * @param saleQuantity
    */
    public int increaseSaledStock(String skuCode, Integer saleQuantity) {
        return productStockMapper.increaseSaledStock(skuCode,saleQuantity);
    }

    /**
     * 还原销售库存
     * @author xuyou
     * 2022/8/6 0006 22:38
     * @param skuCode
     * @param saleQuantity
     * @return int
    */
    public int restoreSaleStock(String skuCode, Integer saleQuantity) {
        return productStockMapper.restoreSaleStock(skuCode,saleQuantity);
    }

    /**
     * 修改商品库存数量
     * @author xuyou
     * 2022/8/6 0006 23:10
     * @param skuCode
     * @param originSaleQuantity
     * @param saleIncremental
     * @return int
    */
    public int modifyProductStock(String skuCode,Long originSaleQuantity,Long saleIncremental) {
        return productStockMapper.modifyProductStock(skuCode,originSaleQuantity,saleIncremental);
    }

}
